<?php
namespace app\common\controller;
use think\Controller;
use think\Db;

class PayBase extends Controller
{
    public function __construct(){
        parent::__construct();
    }
    public function buildHtml($msg='',$from=0){

        $html = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><script language="javascript">';
        $html .= "var u = navigator.userAgent, app = navigator.appVersion;";
        $html .= "var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;";
        $html .= 'var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);';
        if( 1==$from ){
            $html .= "function setupWebViewJavascriptBridge(callback) {
                        if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
                        if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
                        window.WVJBCallbacks = [callback];
                        var WVJBIframe = document.createElement('iframe');
                        WVJBIframe.style.display = 'none';
                        WVJBIframe.src = 'https://__bridge_loaded__';
                        document.documentElement.appendChild(WVJBIframe);
                        setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
                    }";
            $html .= "setupWebViewJavascriptBridge(function(bridge) {
                            bridge.registerHandler('JS Echo', function(data, responseCallback) {
                                responseCallback(data)
                            })
                            bridge.callHandler('backClick', {'key':'value'}, function responseCallback(responseData) {
                            })
                        })".PHP_EOL;
            $html .= "if(isAndroid){
                    jump2app.alertDialog('{$msg}');
                    jump2app.destroyWebActivity();
                }else if(isIOS){
                    alert('{$msg}');
                    WebViewJavascriptBridge.callHandler('backClick');
                }else{
                    alert('浏览器方式未知');
                }";
        }else{
            $html .= "alert('{$msg}');
                if( isAndroid || isIOS ){
                    this.location.replace('/mobile/member/chongzhi');
                }else{
                     this.location.replace('/index/user/recharge');
                }";
        }
        $html .= "</script></html>";
        echo $html;
    }
}
